package com.javaspeak.concurrency.structures.arrayblockingqueue;

import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;

/**
 * @author John Dickerson
 */
public class WriteCallable implements Callable<Result> {

    private ArrayBlockingQueue<QueueObject> arrayBlockingQueue;
    private List<QueueObject> queueObjects;


    public WriteCallable( ArrayBlockingQueue<QueueObject> arrayBlockingQueue,
            List<QueueObject> queueObjects ){

        this.arrayBlockingQueue = arrayBlockingQueue;
        this.queueObjects = queueObjects;
    }


    @Override
    public Result call() throws Exception {

        long nanoBeforeTime = System.nanoTime();

        for ( QueueObject queueObject : queueObjects ) {

            arrayBlockingQueue.put( queueObject );
        }

        long processingNanoTime = System.nanoTime() - nanoBeforeTime;

        Result result =
            new Result( processingNanoTime, queueObjects.size(),
                    Thread.currentThread().getName() );

        return result;
    }
}
